package com.example.crmebshop.zcom.main.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import com.chaychan.library.BottomBarLayout;
import com.chaychan.library.TabData;
import com.example.crmebshop.R;
import com.example.crmebshop.api.ApiConstant;
import com.example.crmebshop.api.ApiRetrofit;
import com.example.crmebshop.api.ApiService;
import com.example.crmebshop.bean.LoginBean;
import com.example.crmebshop.bean.RecommendPoetryBean;
import com.example.crmebshop.bean.SinglePoetryBean;
import com.example.crmebshop.bean.WeatherBean;
import com.example.crmebshop.core.RxHttp;
import com.example.crmebshop.core.RxLife;
import com.example.crmebshop.databinding.ActivityMainBinding;
import com.example.crmebshop.request.RxRequest;
import com.example.crmebshop.request.base.BaseBean;
import com.example.crmebshop.request.exception.ExceptionHandle;
import com.example.crmebshop.request.setting.DefaultRequestSetting;
import com.example.crmebshop.request.setting.ParameterGetter;
import com.example.crmebshop.zcom.base.BaseActivity;
import com.example.crmebshop.zcom.base.BaseFragment;
import com.example.crmebshop.zcom.main.fragment.HomeFragment;
import com.example.crmebshop.zcom.main.fragment.MineFragment;
import com.example.crmebshop.zcom.main.fragment.TabFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;


public class MainActivity extends BaseActivity<ActivityMainBinding> {

    private CompositeDisposable disposables = new CompositeDisposable();
    protected ApiService mApiService = ApiRetrofit.getInstance().getApiService();

    private List<BaseFragment> mFragmentList = new ArrayList<>(4);
    private BottomBarLayout mBottomBarLayout;


    private List<TabData> getTabData() {
        List<TabData> tabData = new ArrayList<>();
        tabData.add(new TabData("首页", R.mipmap.tab_home_normal, R.mipmap.tab_home_selected));
        tabData.add(new TabData("视频", R.mipmap.tab_video_normal, R.mipmap.tab_video_selected));
        tabData.add(new TabData("微头条", R.mipmap.tab_micro_normal, R.mipmap.tab_micro_selected));
        tabData.add(new TabData("我的", R.mipmap.tab_me_normal, R.mipmap.tab_me_selected));
        return tabData;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


//        getSupportActionBar().setTitle(getTitle());




//        RxHttp.init(this);


//        mainBinding.button.setOnClickListener(v -> {
//
//
//        disposables.add(mApiService.mobileLogin("18077398076", "123456")
//                    .subscribeOn(Schedulers.io())
//                    .observeOn(AndroidSchedulers.mainThread())
//                    .subscribe(
//                            resultResponse -> {
//                        // 处理成功响应
//                        Log.d("API", "resultResponse: " + resultResponse.getData());
//
//                        UIUtils.showToastSafely(resultResponse.getMsg());
//                        String data = resultResponse.getData().toString();
//                        JSONObject jsonObject = new JSONObject(data);
//
//                        Log.d("API", "jsonObject-----: " + jsonObject);
//
//
//
//                    }, throwable -> {
//                        // 处理错误
//                        Log.e("API", "Error: " + throwable.getMessage());
//                    }
//
//
//
//                    ));
//        });
        
//        RxHttp.initRequest(new DefaultRequestSetting() {
//            @NonNull
//            @Override
//            public String getBaseUrl() {
////                return ApiConstant.Config.BASE_URL;
//                return ApiConstant.BASE_SERVER_URL;
//            }
//
//            @Override
//            public Map<String, String> getRedirectBaseUrl() {
//                Map<String, String> urls = new HashMap<>(2);
//                urls.put(ApiConstant.Config.BASE_URL_OTHER_NAME, ApiConstant.Config.BASE_URL_OTHER);
//                urls.put(ApiConstant.Config.BASE_URL_ERROR_NAME, ApiConstant.Config.BASE_URL_ERROR);
//                urls.put(ApiConstant.Config.BASE_URL_HTTPS_NAME, ApiConstant.Config.BASE_URL_HTTPS);
//                return urls;
//            }
//
//            @Override
//            public int getSuccessCode() {
//                return ApiConstant.Code.SUCCESS;
//            }
//
//            @Override
//            public Map<String, String> getStaticPublicQueryParameter() {
//                Map<String, String> parameters = new HashMap<>(2);
//                parameters.put("system", "android");
//                parameters.put("version_code", "1");
//                parameters.put("device_num", "666");
//                return parameters;
//            }
//
//            @Override
//            public Map<String, ParameterGetter> getDynamicPublicQueryParameter() {
//                Map<String, ParameterGetter> parameters = new HashMap<>(2);
//                parameters.put("user_id", new ParameterGetter() {
//                    @io.reactivex.annotations.NonNull
//                    @Override
//                    public String get() {
//                        return "100001";
//                    }
//                });
//                return parameters;
//            }
//
//            @Override
//            public void setOkHttpClient(@io.reactivex.annotations.NonNull OkHttpClient.Builder builder) {
//                builder.hostnameVerifier(new HostnameVerifier() {
//                    @Override
//                    public boolean verify(String hostname, SSLSession session) {
//                        return true;
//                    }
//                });
//            }
//        });
//        mRxLife = RxLife.create();


//        mainBinding.tvLogin.setOnClickListener(v->{
//            getLogin();
//        });

    }

    @Override
    public void initView() {
        super.initView();
        mBottomBarLayout = binding.bbl;
    }

    @Override
    protected ActivityMainBinding initViewBinding() {
        return ActivityMainBinding.inflate(getLayoutInflater());
    }


    @Override
    public void initData() {
        super.initData();


//        TabFragment homeFragment = new TabFragment();
//        Bundle bundle1 = new Bundle();
//        bundle1.putString(TabFragment.CONTENT, "首页");
//        homeFragment.setArguments(bundle1);
//        mFragmentList.add(homeFragment);
//
//        TabFragment videoFragment = new TabFragment();
//        Bundle bundle2 = new Bundle();
//        bundle2.putString(TabFragment.CONTENT, "视频");
//        videoFragment.setArguments(bundle2);
//        mFragmentList.add(videoFragment);
//
//        TabFragment microFragment = new TabFragment();
//        Bundle bundle3 = new Bundle();
//        bundle3.putString(TabFragment.CONTENT, "微头条");
//        microFragment.setArguments(bundle3);
//        mFragmentList.add(microFragment);
//
//        TabFragment meFragment = new TabFragment();
//        Bundle bundle4 = new Bundle();
//        bundle4.putString(TabFragment.CONTENT, "我的");
//        meFragment.setArguments(bundle4);
//        mFragmentList.add(meFragment);
        mFragmentList.add(new HomeFragment());
        mFragmentList.add(new HomeFragment());
        mFragmentList.add(new HomeFragment());
        mFragmentList.add(new MineFragment());

        mBottomBarLayout.setData(getTabData());

        changeFragment(0); //默认显示第一页
    }

    @Override
    public void initListener() {
        super.initListener();
        mBottomBarLayout.setOnItemSelectedListener((bottomBarItem, previousPosition, currentPosition) -> {
            Log.i("MainActivity", "position: " + currentPosition);
            changeFragment(currentPosition);
        });

        mBottomBarLayout.setUnread(0, 20);//设置第一个页签的未读数为20
        mBottomBarLayout.setUnread(1, 1001);//设置第二个页签的未读数
        mBottomBarLayout.showNotify(2);//设置第三个页签显示提示的小红点
        mBottomBarLayout.setMsg(3, "NEW");//设置第四个页签显示NEW提示文字
    }


    private void changeFragment(int currentPosition) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fl_content, mFragmentList.get(currentPosition));
        transaction.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_demo, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
//        int id = item.getItemId();
//        switch (id) {
//            case R.id.action_clear_unread:
//                mBottomBarLayout.setUnread(0, 0);
//                mBottomBarLayout.setUnread(1, 0);
//                break;
//            case R.id.action_clear_notify:
//                mBottomBarLayout.hideNotify(2);
//                break;
//            case R.id.action_clear_msg:
//                mBottomBarLayout.hideMsg(3);
//                break;
//        }

        if(item.getItemId() == R.id.action_clear_unread){
            mBottomBarLayout.setUnread(0, 0);
            mBottomBarLayout.setUnread(1, 0);
        } else if(item.getItemId() == R.id.action_clear_notify){
            mBottomBarLayout.hideNotify(2);
        } else if(item.getItemId() == R.id.action_clear_msg){
            mBottomBarLayout.hideMsg(3);
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        disposables.clear();
//        mRxLife.destroy();
    }


//    private void getLogin(){
//        mRxLife.add(RxRequest.create(ApiConstant.api().mobileLogin("18077398076", "123456")).listener(new RxRequest.RequestListener() {
//            private long timeStart = 0;
//
//            @Override
//            public void onStart() {
//                log(null);
//                log("onStart()");
//                timeStart = System.currentTimeMillis();
//            }
//
//            @Override
//            public void onError(@NonNull ExceptionHandle handle) {
//                log("onError(" + handle.getMsg() + ")");
//            }
//
//            @Override
//            public void onFinish() {
//                long cast = System.currentTimeMillis() - timeStart;
//                log("onFinish(cast=" + cast + ")");
//            }
//        }).request(new RxRequest.ResultCallback<LoginBean>() {
//            @Override
//            public void onSuccess(int code, LoginBean data) {
//                log("onSuccess(code=" + code + ",data=" + data.toFormatJson() + ")");
//
//                log("onSuccess(token=" + data.getToken() + ")");
//
//            }
//
//            @Override
//            public void onFailed(int code, String msg) {
//                log("onFailed(code=" + code + ",msg=" + msg + ")");
//            }
//        }));
//    }
//
//    private void log(String text){
//        if (text == null) {
//            tvLog.setText("");
//        } else {
//            Log.d(TAG, text);
//            String textOld = tvLog.getText().toString();
//            if (TextUtils.isEmpty(textOld)) {
//                tvLog.setText(text);
//            } else {
//                tvLog.setText(tvLog.getText().toString() + "\n" + text);
//            }
//        }
//    }

}